package com.lyshop.jinding.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lyshop.jinding.R;
import com.lyshop.jinding.application.MyApplication;
import com.lyshop.jinding.bean.CreateOrderResultBean;
import com.lyshop.jinding.bean.NoviceCouponBean;
import com.lyshop.jinding.bean.OrderInfoBean;
import com.lyshop.jinding.core.AppAction;
import com.lyshop.jinding.net.ActionCallbackListener;
import com.lyshop.jinding.utils.DataUtils;
import com.lyshop.jinding.utils.ToastUtil;
import com.zc.mychart.model.GoodsAllBean;

import java.util.Random;

/**
 * Created by Administrator on 2017/12/20 0020.
 */

public class GuideTicketDialog extends MyDialog implements View.OnClickListener {
    private Context mContext;
    private View mRootView, rl_buy_up, rl_buy_down, iv_check_up, iv_check_down;
    private TextView tv_unit_price;
    private TextView tv_up_user_percent, tv_down_user_percent;
    private Button btn_submit;
    private GuideTicketListener mListener;
    private NoviceCouponBean mNoviceCouponBean;
    private int tradeType = 2;
    private TextView tv_pro_name;
    private TextView tv_pro_price;

    public GuideTicketDialog(Context context, GuideTicketListener listener) {
        super(context);
        mContext = context;
        mListener = listener;
        mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_new_user_ticket, null);
        findviews();
        setContentView(mRootView);
    }


    private void findviews() {
        tv_pro_name = mRootView.findViewById(R.id.tv_pro_name);
        tv_pro_price = mRootView.findViewById(R.id.tv_pro_price);

        rl_buy_up = mRootView.findViewById(R.id.rl_buy_up);
        rl_buy_down = mRootView.findViewById(R.id.rl_buy_down);
        iv_check_up = mRootView.findViewById(R.id.iv_check_up);
        iv_check_down = mRootView.findViewById(R.id.iv_check_down);
        tv_up_user_percent = (TextView) mRootView.findViewById(R.id.tv_up_user_percent);
        tv_down_user_percent = (TextView) mRootView.findViewById(R.id.tv_down_user_percent);
        btn_submit = (Button) mRootView.findViewById(R.id.btn_submit);

        rl_buy_up.setOnClickListener(this);
        rl_buy_down.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    public void setData(NoviceCouponBean noviceCouponBean) {
        mNoviceCouponBean = noviceCouponBean;
        tv_pro_name.setText(noviceCouponBean.name);
        GoodsAllBean goodsAllBean = MyApplication.getApplication().getAllGoodMap().get(noviceCouponBean.contractCode);
        tv_pro_price.setText(goodsAllBean.latest_price + "");
        Random random = new Random();
        int randomNumber = random.nextInt(21); // 生成0到20之间的随机数
        int firstNumber = 40 + randomNumber;
        int secondNumber = 100 - firstNumber;
        tv_up_user_percent.setText(firstNumber + "%");
        tv_down_user_percent.setText(secondNumber + "%");
        show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                createOrder();
                break;
            case R.id.rl_buy_up:
                if (tradeType == 1) {
                    upateTradeType(2);
                }
                break;
            case R.id.rl_buy_down:
                if (tradeType == 2) {
                    upateTradeType(1);
                }
                break;
        }
    }

    private void upateTradeType(int type) {
        tradeType = type;
        if (type == 2) {
            rl_buy_up.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_red);
            rl_buy_down.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_gray);
            iv_check_up.setVisibility(View.VISIBLE);
            iv_check_down.setVisibility(View.GONE);
        } else {
            rl_buy_up.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_gray);
            rl_buy_down.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_green);
            iv_check_up.setVisibility(View.GONE);
            iv_check_down.setVisibility(View.VISIBLE);
        }
    }

    private void createOrder() {
        mListener.onLoading();
        OrderInfoBean order = new OrderInfoBean();
        order.goods_id = mNoviceCouponBean.useProductId;
        order.trade_type = tradeType;
        order.amount = 1;
        order.use_ticket = 1;
        order.target_profit = Integer.parseInt(mNoviceCouponBean.couponMaxProfitPoint);
        order.stop_loss = Integer.parseInt(mNoviceCouponBean.couponMaxStopPoint);
        order.isFirst = 1;
        AppAction appAction = ((MyApplication) mContext.getApplicationContext()).getAppAction();
        appAction.createPro(order, new ActionCallbackListener<CreateOrderResultBean>() {
            @Override
            public void onSuccess(CreateOrderResultBean data) {
                mListener.onLoadingCancel();
                DataUtils.setGuideBuy(mContext, true);
                mListener.onSuccess();
                dismiss();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                mListener.onLoadingCancel();
                ToastUtil.showToast(mContext, message);
                mListener.onFailed();
            }
        });
    }


    public void refreshProPrice() {
        MyApplication myApplication = (MyApplication) mContext.getApplicationContext();
        GoodsAllBean priceBean = myApplication.getAllGoodMap().get(mNoviceCouponBean.contractCode);
        tv_pro_price.setText(priceBean.latest_price + "");
    }

    public interface GuideTicketListener {
        void onSuccess();

        void onFailed();

        void onCancel();

        void onLoading();

        void onLoadingCancel();

    }
}
